﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace EMR
{
    public partial class frm_ManageCaseModel : Form
    {
        string filename = "../../TemplateStore";
        public frm_ManageCaseModel()
        {
            InitializeComponent();
        }
      
        public  void addcolumns()
        {
            lvw_Template.Columns.Add("模板名称", 100);
            lvw_Template.Columns.Add("是否必须", 100);
            lvw_Template.Columns.Add("是否多份", 100);
            lvw_Template.Columns.Add("适用性别", 100);
            lvw_Template.Columns.Add("备注", 100);
        }
        private void frm_ManageCaseModel_Load(object sender, EventArgs e)
        {

            loadtree();

        }

        private void loadtree()
        {
            lvw_Template.View = View.Details;
           

            try
            {
                tvw_Template.Nodes.Clear(); //清空TreeView

                DirectoryInfo dirs = new DirectoryInfo(filename); //获得程序所在路径的目录对象
                DirectoryInfo[] dir = dirs.GetDirectories();//获得目录下文件夹对象
                FileInfo[] file = dirs.GetFiles();//获得目录下文件对象
                int dircount = dir.Count();//获得文件夹对象数量
                int filecount = file.Count();//获得文件对象数量

                //循环文件夹
                for (int i = 0; i < dircount; i++)
                {
                    tvw_Template.Nodes.Add(dir[i].Name);
                    string pathNode = filename + "\\" + dir[i].Name;
                    GetMultiNode(tvw_Template.Nodes[i], pathNode);
                }

                //循环文件
                for (int j = 0; j < filecount; j++)
                {
                    tvw_Template.Nodes.Add(file[j].Name);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message + "\r\n出错的位置为：Form1.PaintTreeView()");
            }
        }

        private bool GetMultiNode(TreeNode treeNode, string path)
        {
            if (Directory.Exists(path) == false)
            { return false; }

            DirectoryInfo dirs = new DirectoryInfo(path); //获得程序所在路径的目录对象
            DirectoryInfo[] dir = dirs.GetDirectories();//获得目录下文件夹对象
            FileInfo[] file = dirs.GetFiles();//获得目录下文件对象
            int dircount = dir.Count();//获得文件夹对象数量
            int filecount = file.Count();//获得文件对象数量
            int sumcount = dircount + filecount;

            if (sumcount == 0)
            { return false; }

            //循环文件夹
            for (int j = 0; j < dircount; j++)
            {
                treeNode.Nodes.Add(dir[j].Name);
                string pathNodeB = path + "\\" + dir[j].Name;
                GetMultiNode(treeNode.Nodes[j], pathNodeB);
            }

            //循环文件
            for (int j = 0; j < filecount; j++)
            {
                treeNode.Nodes.Add(file[j].Name);
            }
            return true;
        }

        private void tvw_Template_AfterSelect(object sender, TreeViewEventArgs e)
        {
            if (this.tvw_Template.SelectedNode.Text != "专科模板" && tvw_Template.SelectedNode.Text != "标准模板")
            {
                lvw_Template.Clear();
                addcolumns();
                //MessageBox.Show(tvw_Template.SelectedNode.Text.Replace(".xml", ""));
                var dr = TemplateBll.Load(tvw_Template.SelectedNode.Text.Replace(".xml", ""));
                ListViewItem item = new ListViewItem(dr.Name);
                //给每一项里面添加信息
                item.SubItems.AddRange(new string[] { (dr.IsMultiple==1? "是" : "否")  , dr.IsMust == 1 ? "是" : "否" , dr.AppliesGender, dr.Remark });
                lvw_Template.Items.Add(item);
                //int                                                                                             //声明整型变量，并调用数据阅读器的方法GetOrdinal获取各列相应的索引值；
                //         nameOrdinal = dr.GetOrdinal("Name")
                //        , genderOrdinal = dr.GetOrdinal("AppliesGender")
                //        , ismultipleOrdinal = dr.GetOrdinal("IsMultiple")
                //        , ismustOrdinal = dr.GetOrdinal("IsMust")
                //        , remarkOrdinal = dr.GetOrdinal("Remark");
                //if (dr.Read())                                                                       //若数据阅读器成功读取到下一条记录（首次查询则表示第一条记录）；
                //{
                //    string name = dr.GetString(nameOrdinal);
                //    string ismultiple = dr.GetBoolean(ismultipleOrdinal) == true ? "是" : "否";
                //    string ismust = dr.GetBoolean(ismustOrdinal) == true ? "是" : "否"; ;
                //    string AppliesGender = dr.GetString(genderOrdinal);
                //    string Remark = dr.GetString(remarkOrdinal);
                //    //创建项
                //    ListViewItem item = new ListViewItem(name);
                //    //给每一项里面添加信息
                //    item.SubItems.AddRange(new string[] { ismultiple, ismust, AppliesGender, Remark });
                //    lvw_Template.Items.Add(item);
                //}

            }
        }
        private void 删除ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (this.tvw_Template.SelectedNode.Text != "专科模板" && tvw_Template.SelectedNode.Text != "标准模板"  && tvw_Template.SelectedNode != null )
            {
                //MessageBox.Show(tvw_Template.SelectedNode.Text.Replace(".xml", ""));
                Template template = new Template();
                template.Name = tvw_Template.SelectedNode.Text.Replace(".xml", "");
                TemplateBll.DeleteTemplate(template);
                string pa = filename + '/' + tvw_Template.SelectedNode.Parent.Text + '/' + tvw_Template.SelectedNode.Text;
                //TreeNode tree = tvw_Template.SelectedNode.Parent;
                // MessageBox.Show(tree.Text);
                File.Delete(pa);
                lvw_Template.Clear();
                loadtree();
                tvw_Template.ExpandAll();
                //tree.ExpandAll();

            }
            else
            {
                MessageBox.Show("请选择正确的模板！"); 
            }
           
        }

        private void 添加ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            AddTemplate addform = new AddTemplate();
            addform.ShowDialog();
            addform.StartPosition = FormStartPosition.CenterScreen;
            if (addform.DialogResult == DialogResult.OK)
            {
               // string pa = filename + '/' + tvw_Template.SelectedNode.Parent.Text + '/' + tvw_Template.SelectedNode.Text;
                //TreeNode tree = tvw_Template.SelectedNode.Parent;
                // MessageBox.Show(tree.Text);
               
                //File.Delete(pa);
                lvw_Template.Clear();
                loadtree();
                tvw_Template.ExpandAll();
               
            }

        }

        private void tvw_Template_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            if (this.tvw_Template.SelectedNode.Text != "专科模板" && tvw_Template.SelectedNode.Text != "标准模板")
            {
                TemplateDesign template = new TemplateDesign(tvw_Template.SelectedNode.FullPath.ToString());

                template.ShowDialog();

            }
        }

        private void 编辑ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (this.tvw_Template.SelectedNode.Text != "专科模板" && tvw_Template.SelectedNode.Text != "标准模板" && tvw_Template.SelectedNode != null)
            {
                //MessageBox.Show(tvw_Template.SelectedNode.Text.Replace(".xml", ""));
                Template template = new Template();
                template.Name = tvw_Template.SelectedNode.Text.Replace(".xml", "");
                EditTemplate editTemplate = new EditTemplate(template.Name, this.tvw_Template.SelectedNode.Parent.Text);
                editTemplate.ShowDialog();
                string pa = filename + '/' + tvw_Template.SelectedNode.Parent.Text + '/' + tvw_Template.SelectedNode.Text;
                //TreeNode tree = tvw_Template.SelectedNode.Parent;
                // MessageBox.Show(tree.Text);
                lvw_Template.Clear();
                loadtree();
                tvw_Template.ExpandAll();
                //tree.ExpandAll();

            }
            else
            {
                MessageBox.Show("请选择正确的模板！");
            }
        }
    }
}
